package com.htudata.core.util.operation;

import lombok.Getter;
import lombok.Setter;

import java.util.Arrays;
import java.util.List;

public class ListPagingOperationUtils<T> {

  @Getter
  @Setter
  private List<T> data;
  @Getter
  @Setter
  private int lastPage;
  @Getter
  @Setter
  private int nowPage;
  @Getter
  @Setter
  private int nextPage;
  @Getter
  @Setter
  private int pageSize;
  @Getter
  @Setter
  private int totalPage;
  @Getter
  @Setter
  private int totalCount;
  

  public ListPagingOperationUtils(List<T> data, int nowPage, int pageSize) {
    if (data == null || data.isEmpty())
      throw new IllegalArgumentException("data must be not empty!"); 
    this.data = data;
    this.pageSize = pageSize;
    this.nowPage = nowPage;
    this.totalCount = data.size();
    this.totalPage = (this.totalCount + pageSize - 1) / pageSize;
    this.lastPage = (nowPage - 1 > 1) ? (nowPage - 1) : 1;
    this.nextPage = (nowPage >= this.totalPage) ? this.totalPage : (nowPage + 1);
  }
  
  public List<T> getPagedList() {
    int fromIndex = (this.nowPage - 1) * this.pageSize;
    if (fromIndex >= this.data.size())
      return this.data; 
    if (fromIndex < 0)
      return this.data; 
    int toIndex = this.nowPage * this.pageSize;
    if (toIndex >= this.data.size())
      toIndex = this.data.size(); 
    return this.data.subList(fromIndex, toIndex);
  }

}
